# !/usr/bin/env python
# -*- coding: utf-8 -*-


import concurrent.futures


# 要在线程中运行的函数
def worker(n):
    return n * n


# 主程序
def main():
    # 创建一个线程池，最大线程数为4
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        # 提交任务到线程池中
        future_to_result = {executor.submit(worker, n): n for n in range(10)}
        print("future_to_result", future_to_result)
        # 获取结果
        for future in concurrent.futures.as_completed(future_to_result):
            n = future_to_result[future]
            try:
                data = future.result()
                print(f'{n} squared: {data}')
            except Exception as e:
                print(f'{n} generated an exception: {e}')


if __name__ == '__main__':
    main()
